package com.bestv.search.management.command;

import com.bestv.search.common.dataSource.DataSourceKey;
import com.bestv.search.common.model.SysUser;
import com.bestv.search.common.model.TargetType;
import com.bestv.search.common.service.TargetTypeManager;
import com.bestv.search.common.util.CommandException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TargetTypeCommand {

    protected final Log log = LogFactory.getLog(getClass());
    private TargetTypeManager targetTypeManager;

    public String find(SysUser user, Map<String, String[]> map, DataSourceKey dsKey) throws CommandException {
        String result = "";

        List<TargetType> types = targetTypeManager.getTargetTypes(dsKey);
        try {
            JSONArray jsonArray = targetTypesToJson(types);
            result = "{'targetTypes':" + jsonArray.toString() + "}";
        } catch (JSONException e) {
            log.error(e);
            throw new CommandException();
        }
        return result;
    }

    public String get(SysUser user, Map<String, String[]> map, DataSourceKey dsKey) throws CommandException {
        String result = "";
        String[] id = map.get("id");

        TargetType t = targetTypeManager.getTargetById(dsKey, new Long(id[0]));
        try {
            JSONObject jsonObj = targetTypeToJson(t);
            result = jsonObj.toString();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            log.error(e);
            throw new CommandException();
        }
        return result;
    }

    public String save(SysUser user, Map<String, String[]> map, DataSourceKey dsKey) {
        String result = "";
        String[] id = map.get("id");
        String[] name = map.get("typeName");
        String[] description = map.get("typeDescription");

        TargetType type = null;
        if ("".equals(id[0])) {
            // new
            TargetType t = targetTypeManager.getTargetByName(dsKey, name[0]);
            if (t != null)
                return "{'result':'failure','message':'name'}";
            type = new TargetType();
            type.setName(name[0]);
            type.setDescription(description[0]);
            type.setId(0L);
            targetTypeManager.saveTargetType(dsKey, type);
            result = "{'result':'success','message':'new'}";
        } else {
            type = targetTypeManager.getTargetById(dsKey, new Long(id[0]));
            if (type == null)
                type = new TargetType();
            type.setName(name[0]);
            type.setDescription(description[0]);
            targetTypeManager.saveTargetType(dsKey, type);
            result = "{'result':'success','message':'edit'}";
        }
        return result;
    }

    public String delete(SysUser user, Map<String, String[]> map, DataSourceKey dsKey) {
        String result = "";
        String[] id = (String[]) map.get("id");

        try {
            targetTypeManager.deleteTargetType(dsKey, new Long(id[0]));
            result = "{'result':'success','message':''}";
        } catch (NumberFormatException e) {
            log.error(e);
        } catch (CommandException e) {
            log.error(e);
        }
        return result;
    }

    private JSONObject targetTypeToJson(TargetType t) throws JSONException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", t.getId());
        jsonObject.put("name", t.getName());
        jsonObject.put("description", t.getDescription());
        return jsonObject;
    }

    private JSONArray targetTypesToJson(List<TargetType> targetTypes) throws JSONException {
        List<JSONObject> list = new ArrayList<JSONObject>();
        for (TargetType t : targetTypes) {
            list.add(targetTypeToJson(t));
        }
        JSONArray jsonArray = new JSONArray(list.toArray());
        return jsonArray;
    }

    public TargetTypeManager getTargetTypeManager() {
        return targetTypeManager;
    }

    public void setTargetTypeManager(TargetTypeManager targetTypeManager) {
        this.targetTypeManager = targetTypeManager;
    }
}
